/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class NodePreDisplacementsDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private String gAnlType;
	
	private DecimalField DxField;
	private DecimalField DyField;
	private DecimalField DzField;
	private DecimalField RxField;
	private DecimalField RyField;
	private DecimalField RzField;
	public double Dx;
	public double Dy;
	public double Dz;
	public double Rx;
	public double Ry;
	public double Rz;
	
//*****************************************************************************
	
	public NodePreDisplacementsDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		gAnlType = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		
		setUpFormat();
		setUpFields();
		
		JPanel displacePane = new JPanel();
		displacePane.setPreferredSize(new Dimension(360, 280));
		displacePane.setLayout(new GridLayout(4,1,2,2));
		JPanel displaceDPane = new JPanel();
		displaceDPane.setLayout(new GridLayout(1,3,2,2));
		displacePane.add(displaceDPane);
		JPanel displaceValueDPane = new JPanel();
		displaceValueDPane.setLayout(new GridLayout(1,3,2,2));
		displacePane.add(displaceValueDPane);
		JPanel displaceRPane = new JPanel();
		displaceRPane.setLayout(new GridLayout(1,3,2,2));
		displacePane.add(displaceRPane);
		JPanel displaceValueRPane = new JPanel();
		displaceValueRPane.setLayout(new GridLayout(1,3,2,2));
		displacePane.add(displaceValueRPane);
		
		displaceDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDx64.gif")) ));
		displaceDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDy64.gif")) ));
		displaceDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDz64.gif")) ));
		displaceRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRx64.gif")) ));
		displaceRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRy64.gif")) ));
		displaceRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRz64.gif")) ));
		
		JPanel displaceDxPane = new JPanel();
		JPanel displaceDyPane = new JPanel();
		JPanel displaceDzPane = new JPanel();
		JPanel displaceRxPane = new JPanel();
		JPanel displaceRyPane = new JPanel();
		JPanel displaceRzPane = new JPanel();
		
		displaceDxPane.add(new JLabel("Dx:"));
		displaceDyPane.add(new JLabel("Dy:"));
		displaceDzPane.add(new JLabel("Dz:"));
		displaceRxPane.add(new JLabel("Rx:"));
		displaceRyPane.add(new JLabel("Ry:"));
		displaceRzPane.add(new JLabel("Rz:"));
		
		displaceDxPane.add(DxField);
		displaceDyPane.add(DyField);
		displaceDzPane.add(DzField);
		displaceRxPane.add(RxField);
		displaceRyPane.add(RyField);
		displaceRzPane.add(RzField);
		
		displaceValueDPane.add(displaceDxPane);
		displaceValueDPane.add(displaceDyPane);
		displaceValueDPane.add(displaceDzPane);
		displaceValueRPane.add(displaceRxPane);
		displaceValueRPane.add(displaceRyPane);
		displaceValueRPane.add(displaceRzPane);
		
		if (gAnlType.equals("PlaneFrame")) {
			DzField.setEnabled(false);
			RxField.setEnabled(false);
			RyField.setEnabled(false);
		}
		else if (gAnlType.equals("PlaneTruss")) {
			DzField.setEnabled(false);
			RxField.setEnabled(false);
			RyField.setEnabled(false);
			RzField.setEnabled(false);
		}
		else if (gAnlType.equals("Beam")) {
			DxField.setEnabled(false);
			DzField.setEnabled(false);
			RxField.setEnabled(false);
			RyField.setEnabled(false);
		}
		else if (gAnlType.equals("Grid")) {
			DxField.setEnabled(false);
			DzField.setEnabled(false);
			RyField.setEnabled(false);
		}
		
		setPane(displacePane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dNPDispDialog01"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			Dx = DxField.getValue();
			Dy = DyField.getValue();
			Dz = DzField.getValue();
			Rx = RxField.getValue();
			Ry = RyField.getValue();
			Rz = RzField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		if (this.parent.getController().getSelection().size() == 1 ) {
			Node node = (Node)this.parent.getController().getSelection().get(0);
			
			DxField = new DecimalField(node.getPreDisplacement(0), 7, numberFormat);
			DyField = new DecimalField(node.getPreDisplacement(1), 7, numberFormat);
			DzField = new DecimalField(node.getPreDisplacement(2), 7, numberFormat);
			RxField = new DecimalField(node.getPreDisplacement(3), 7, numberFormat);
			RyField = new DecimalField(node.getPreDisplacement(4), 7, numberFormat);
			RzField = new DecimalField(node.getPreDisplacement(5), 7, numberFormat);
			
		} else {
			
			DxField = new DecimalField(0, 7, numberFormat);
			DyField = new DecimalField(0, 7, numberFormat);
			DzField = new DecimalField(0, 7, numberFormat);
			RxField = new DecimalField(0, 7, numberFormat);
			RyField = new DecimalField(0, 7, numberFormat);
			RzField = new DecimalField(0, 7, numberFormat);
			
		}
	}
	
//*****************************************************************************
}
